﻿using ELab.Client;
using ELab.Communication;
using ELab.Serialization.Data;
using ELab.Settings;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ELab.Teacher
{
    public partial class frmLogin : Form
    {
        public bool IsSuccess { get; private set; }
        public frmLogin()
        {
            InitializeComponent();

            var serverInfor = ELabSettings.Instance.Servers.FirstOrDefault();
            string ipAddressServer = serverInfor == null ? CommunicationConsts.DefaultIpAddress : serverInfor.IpAddress;
            int portServer = serverInfor == null ? CommunicationConsts.DefaultPort : serverInfor.Port;
            var controller = new ELab.Management.ELabController(ipAddressServer, portServer);
            this.uLoginControl.Controller = controller;
            this.IsSuccess = false;
            this.uLoginControl.UserType = UserType.Teacher;
            this.uLoginControl.OnCancelLogin += uLoginControl_OnCancelLogin;
            this.uLoginControl.OnLoginSuccess += uLoginControl_OnLoginSuccess;
            this.uLoginControl.OnLoginAnotherMachine += uLoginControl_OnLoginAnotherMachine;
        }

        void uLoginControl_OnLoginAnotherMachine(object sender, EventArgs e)
        {
            //Do add service check
            if (true) //if service ok
            {
                this.IsSuccess = true;
            }
            else
            {
                this.IsSuccess = false;
            }
            this.Close();
        }

        void uLoginControl_OnLoginSuccess(object sender, EventArgs e)
        {
            this.IsSuccess = true;
            this.Close();
        }

        void uLoginControl_OnCancelLogin(object sender, EventArgs e)
        {
            this.IsSuccess = false;
            this.Close();
        }
    }
}
